package com.hyf.translucentstatusbar.base;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.design.internal.NavigationMenuView;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.Target;
import com.hyf.translucentstatusbar.R;
import com.hyf.translucentstatusbar.util.StatusBarUtils;

import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * Created by Administrator on 2017/9/14.
 */

class BaseAbstractActivity extends AppCompatActivity {

    /**
     * 白色字体
     */
    protected static final int TOOL_BAR_DARK = 0;
    /**
     * 黑色字体
     */
    protected static final int TOOL_BAR_LIGHT = 1;
    /**
     * 默认的toolbar模式
     */
    private static int TOOL_BAR_MODE = TOOL_BAR_DARK;
    /**
     * 默认未设置headerbar颜色的颜色
     */
    protected int TITLE_BAR_DEFAULT_COLOR = Color.parseColor("#cccccc");

    private ImageView toolBarImage;
    protected Toolbar toolBar;
    private RelativeLayout rootView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    public void setContentView(int layoutResID) {
        setContentView(getLayoutInflater().inflate(layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
        View v = getLayoutInflater().inflate(R.layout.hyf_activity_base_header, null);
        rootView = (RelativeLayout) v.findViewById(R.id.hyf_container);
        toolBar = (Toolbar) v.findViewById(onHeaderBarMode() == TOOL_BAR_LIGHT ? R.id.hyf_light_toolbar : R.id.hyf_dark_toolbar);
        toolBar.setVisibility(View.VISIBLE);
        toolBar.setBackgroundColor(Color.TRANSPARENT);
        toolBarImage = (ImageView) v.findViewById(R.id.hyf_iv_title_bar);
        toolBarImage.setVisibility(View.VISIBLE);

        rootView.addView(view, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        getWindow().setContentView(v);
        // 加一个和状态栏宽高一样的view放在decorView下面，通过第二个参数设置透明度（0~255）
        StatusBarUtils.setTranslucentImageHeader(this, onHeaderBarStatusViewAlpha(), toolBar);
        initHeaderImage();

        setHeaderImage(onHeaderBarImage());
    }

    private void initHeaderImage() {
        int marginBot = toolBarImage.getLayoutParams().height - (toolBar.getLayoutParams().height + getStatusBarHeight());
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolBarImage.getLayoutParams();
        params.setMargins(0, -marginBot, 0, 0);
        toolBarImage.setLayoutParams(params);
    }

    // -----------------------------------------------一些设置方法--------------------------------------------

    /**
     * @param alpha 0~255
     */
    protected final void setHeaderBarAlpha(@IntRange(from = 0, to = 255) int alpha) {
        if (toolBarImage == null) return;
        toolBarImage.setAlpha((float) alpha / 255f);
    }


    protected final void setHeaderBarEnable(boolean enable) {
        if (toolBarImage == null)
            return;
        RelativeLayout view = getHeaderBarParentView();
        view.setVisibility(enable ? View.VISIBLE : View.INVISIBLE);
    }

    protected void setDisplayHomeAsUpEnabled() {
        setDisplayHomeAsUpEnabled(TOOL_BAR_MODE == TOOL_BAR_LIGHT ? TITLE_BAR_DEFAULT_COLOR : Color.WHITE);
    }

    protected void setDisplayHomeAsUpEnabled(int color) {
        Drawable drawable = getResources().getDrawable(R.drawable.hyf_ic_arrow_back_black_24dp);
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        toolBar.setNavigationIcon(drawable);
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setNavigationOnClickListener();
            }
        });
    }

    /**
     * 点击返回的事件
     */
    protected void setNavigationOnClickListener() {
        finish();
    }

    /**
     * 设置headerbar加载的图片
     *
     * @param image
     */
    protected final void setHeaderImage(Object image) {
        // 高斯模糊背景 原来 参数：12,5  23,4
        Glide.with(this)
                .load(image)
                .error(onHeaderBarDrawable())
                .bitmapTransform(new BlurTransformation(this, 23, 4))
                .into(new GlideDrawableImageViewTarget(toolBarImage));
    }


    /**
     * 隐藏/显示NavigationView侧边栏滑动的时候的滚动条
     *
     * @param nav    NavigationView对象
     * @param enable 是否显示/隐藏
     */
    protected final void setNavigationScrollbarsEnable(NavigationView nav, boolean enable) {
        if (nav == null)
            return;
        NavigationMenuView menuView = (NavigationMenuView) nav.getChildAt(0);
        if (menuView == null)
            return;
        menuView.setVerticalScrollBarEnabled(enable);
    }

    // -----------------------------------------------配置方法--------------------------------------------

    /**
     * 配置状态栏透明度
     *
     * @return
     */
    @IntRange(from = 0, to = 255)
    protected int onHeaderBarStatusViewAlpha() {
        return 50;
    }

    /**
     * 配置toolbar样式
     *
     * @return
     */
    protected int onHeaderBarMode() {
        return TOOL_BAR_MODE;
    }

    /**
     * 配置headerbar默认显示的图片
     *
     * @return
     */
    protected Object onHeaderBarImage() {
        return "";
    }

    /**
     * 配置headerbar默认显示的drawable
     *
     * @return
     */
    protected Drawable onHeaderBarDrawable() {
        return new ColorDrawable(TITLE_BAR_DEFAULT_COLOR);
    }

    /**
     *  获取滑动
     */

    // -----------------------------------------------获取方法--------------------------------------------


    /**
     * 获取Headerbar的父控件
     *
     * @return
     */
    protected final RelativeLayout getHeaderBarParentView() {
        if (toolBarImage == null) return null;
        return (RelativeLayout) toolBarImage.getParent();
    }

    /**
     * 获取状态栏高度
     *
     * @return 状态栏高度
     */
    protected final int getStatusBarHeight() {
        // 获得状态栏高度
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }

    // -----------------------------------------------抽象方法--------------------------------------------


    // -----------------------------------------------废弃方法--------------------------------------------

    /**
     * @return
     * @deprecated 已废弃 不再起作用 请使用
     * {{@link #onHeaderBarStatusViewAlpha()}}
     */
    @Deprecated
    protected boolean setHeaderBarStatus() {
        return true;
    }

    /**
     * @return
     * @deprecated 已废弃 不再起作用 请使用
     * {{@link #onHeaderBarImage()}}
     */
    @Deprecated
    protected Object setDefaultHeaderImage() {
        return "";
    }

}
